#include <stc8h.h>
#include <intrins.h>
#include "stdio.h"
#include "fun.h"
bit B1S_Flag;

char code numcode[]={
    0x7B,       /*'0', 0*/
    0x28,       /*'1', 1*/
    0x5E,       /*'2', 2*/
    0x6E,       /*'3', 3*/
    0x2D,       /*'4', 4*/
    0x67,       /*'5', 5*/
    0x77,       /*'6', 6*/
    0x2A,       /*'7', 7*/
    0x7F,       /*'8', 8*/
    0x6F,       /*'9', 9*/
};
void showsec(unsigned char n){
	char cts;

	
	cts=numcode[n%10];

	C0SEGV3&=0x0f;
	C1SEGV3&=0x0f;
	C2SEGV3&=0x0f;
	C3SEGV3&=0x0f;
	C0SEGV3|=((cts&0x03)<<6);
	cts=cts>>2;
	C1SEGV3|=((cts&0x03)<<6);
	cts=cts>>2;
	C2SEGV3|=((cts&0x03)<<6);
	cts=cts>>2;
	C3SEGV3|=((cts&0x03)<<6);

	cts=numcode[n/10];

	C0SEGV3|=(cts&0x03)<<4;
	cts=cts>>2;
	C1SEGV3|=(cts&0x03)<<4;
	cts=cts>>2;
	C2SEGV3|=(cts&0x03)<<4;
	cts=cts>>2;
	C3SEGV3|=(cts&0x03)<<4;
}
void showm(char m,char h){
	char cts;
	
	C0SEGV3&=0xf3;
	C1SEGV3&=0xf3;
	C2SEGV3&=0xf3;
	C3SEGV3&=0xf3;
	C0SEGV2&=0x03;
	C1SEGV2&=0x03;
	C2SEGV2&=0x03;
	C3SEGV2&=0x03;

	cts=numcode[m%10];
	C0SEGV3|=(cts&0x03)<<2;
	cts=cts>>2;
	C1SEGV3|=(cts&0x03)<<2;
	cts=cts>>2;
	C2SEGV3|=(cts&0x03)<<2;
	cts=cts>>2;
	C3SEGV3|=(cts&0x03)<<2;

	cts=numcode[m/10];
	C0SEGV2|=((cts&0x03)<<6);
	cts=cts>>2;
	C1SEGV2|=((cts&0x03)<<6);
	cts=cts>>2;
	C2SEGV2|=((cts&0x03)<<6);
	cts=cts>>2;
	C3SEGV2|=((cts&0x03)<<6);

	cts=numcode[h%10];
	C0SEGV2|=(cts&0x03)<<4;
	cts=cts>>2;
	C1SEGV2|=(cts&0x03)<<4;
	cts=cts>>2;
	C2SEGV2|=(cts&0x03)<<4;
	cts=cts>>2;
	C3SEGV2|=(cts&0x03)<<4;

	cts=numcode[h/10];
	C0SEGV2|=(cts&0x03)<<2;
	cts=cts>>2;
	C1SEGV2|=(cts&0x03)<<2;
	cts=cts>>2;
	C2SEGV2|=(cts&0x03)<<2;
	cts=cts>>2;
	C3SEGV2|=(cts&0x03)<<2;
}
void Delay1000ms(void)	//@11.0592MHz
{
	unsigned char data i, j, k;

	i = 57;
	j = 27;
	k = 112;
	do
	{
		do
		{
			while (--k);
		} while (--j);
	} while (--i);
}
void RTC_Isr() interrupt RTC_VECTOR
{

    P_SW2 |= 0x80;                              //使能XFR访问

    if(RTCIF & 0x08)                            //判断是否秒中断
    {
        RTCIF &= ~0x08;                         //清中断标志
        B1S_Flag = 1;
    }

}

void RTC_init(void){

	X32KCR=0x80; //开启外部 32K晶振
	// IRC32KCR =0x80; //开启内部32k
    P1IE &= ~0xc0;     //关闭数字输入
    INIYEAR = 25;                               //Y:2021
    INIMONTH = 4;                              //M:12
    INIDAY = 5;                                //D:31
    INIHOUR = 23;                               //H:23
    INIMIN = 59;                                //M:59
    INISEC = 50;                                //S:50
    INISSEC = 0;                                //S/128:0
    RTCCFG = 0x01;                             //触发RTC寄存器初始化

    RTCIF = 0;                                  //清中断标志
    RTCIEN = 0x08;                              //使能RTC秒中断
    RTCCR = 1;  

}

void main(void){
    P_SW2 |= 0x80; //使能XFR访问

    // P0M0 = 0x00; P0M1 = 0xff; 
    // P1M0 = 0x00; P1M1 = 0x00; 
    // P2M0 = 0x00; P2M1 = 0xff; 
    // P3M0 = 0x00; P3M1 = 0x60; 
    // P4M0 = 0x00; P4M1 = 0x7e; 
    // P5M0 = 0x00; P5M1 = 0x0f; 
	// P6M0 = 0x00; P6M1 = 0x00; 
    // P7M0 = 0x00; P7M1 = 0x00; 


    P0M0 = 0x00; P0M1 = 0x00; 
    P1M0 = 0x00; P1M1 = 0xc0; 
    P2M0 = 0x00; P2M1 = 0x00; 
    P3M0 = 0x00; P3M1 = 0x00; 
    P4M0 = 0x00; P4M1 = 0x00; 
    P5M0 = 0x00; P5M1 = 0x00; 
	P6M0 = 0x00; P6M1 = 0x00; 
    P7M0 = 0x00; P7M1 = 0x00; 









	Uart1_Init();
    RTC_init();

    
	LCDCFG = 0x83;
	DBLEN=2;
	COMLENH=0x00;
	COMLENM=0x00;
	COMLENL=0x65;
	BLINKRATE=60;
	COMON  = 0x0f;                // COM使能寄存器
	SEGON1 = 0x80;                // SEG线使能寄存器1, SEG7~SEG0
	SEGON2 = 0xff;                // SEG线使能寄存器2, SEG15~SEG8
	SEGON3 = 0xff;                // SEG线使能寄存器3, SEG23~SEG16
	SEGON4 = 0xff;                // SEG线使能寄存器4, SEG31~SEG24
	SEGON5 = 0x00;                // SEG线使能寄存器5, SEG39~SEG32





    P5M0 &= ~0x0f; P5M1 |= 0x0f; 

    P4M0 &= ~0x7e; P4M1 |= 0x7e; 

    P3M0 &= ~0xe0; P3M1 |= 0xe0;  
    P2M0 = 0x00; P2M1 = 0xff; 
    P0M0 = 0x00; P0M1 = 0xff; 

    P5IE &= ~0x0f; 

    P4IE &= ~0x7e; 

    P3IE &= ~0xe0;  
    P2IE = 0x00; 
    P0IE = 0x00; 

    LCDCR=0x01;


    C0SEGV0=0x0f;
    C0SEGV1=0x0f;
    C0SEGV2=0x0f;
    C0SEGV3=0x0f;
    C1SEGV0=0x0f;
    C1SEGV1=0x0f;
    C1SEGV2=0x0f;
    C1SEGV3=0x0f;
    C2SEGV0=0x0f;
    C2SEGV1=0x0f;
    C2SEGV2=0x0f;
    C2SEGV3=0x0f;
    C3SEGV0=0x0f;
    C3SEGV1=0x0f;
    C3SEGV2=0x0f;
    C3SEGV3=0x0f;
    

    UartSendStr("start");
	Delay1000ms();
	Delay1000ms();
	Delay1000ms();
	Delay1000ms();
	Delay1000ms();
	while(1){
        if(B1S_Flag)
        {
            showsec(SEC);
            showm(MIN,HOUR);
            B1S_Flag = 0;
            UartSend(SEC);
            _nop_(); _nop_(); _nop_();
            PCON=PD;
            _nop_(); _nop_(); _nop_();
        }
		runcmd();
		
    }

}